*** NOTE:  This do-file performs the specifications in Table 11b with APL = flip_duration ***



program mymlogit_uh_lf
	version 10.1
	args lnf bxj1 bxj2 loc_j1_l1 loc_j1_l2 mass_point loc_j2_l1 loc_j2_l2
	tempvar denom_l1 denom_l2 mass_prob_l1 mass_prob_l2
	quietly generate double `denom_l1' = 1 + exp(`bxj1' + `loc_j1_l1') + exp(`bxj2' + `loc_j2_l1')
	quietly generate double `denom_l2' = 1 + exp(`bxj1' + `loc_j1_l2') + exp(`bxj2' + `loc_j2_l2')

	quietly generate double `mass_prob_l1' = exp(`mass_point') / (1 + exp(`mass_point'))
	quietly generate double `mass_prob_l2' = 1 / (1 + exp(`mass_point'))


	quietly replace `lnf' = ln((`mass_prob_l1'*(1/`denom_l1')) + (`mass_prob_l2'*(1/`denom_l2'))) if $ML_y1 == 0
	quietly replace `lnf' = ln((`mass_prob_l1'*(exp(`bxj1' + `loc_j1_l1')/`denom_l1')) + (`mass_prob_l2'*(exp(`bxj1' + `loc_j1_l2')/`denom_l2'))) if $ML_y1 == 1
	quietly replace `lnf' = ln((`mass_prob_l1'*(exp(`bxj2' + `loc_j2_l1')/`denom_l1')) + (`mass_prob_l2'*(exp(`bxj2' + `loc_j2_l2')/`denom_l2'))) if $ML_y1 == 2
end



log using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Logs\20110231 APL regs -- original size -- refi_frm -- flip_duration.log", replace



***********************
***********************
*** APL regressions ***
*** refi_frm        ***
*** flip_duration   ***
***********************
***********************



use "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\apl law variables for stata.dta", clear
sort origmonth state
save "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\apl law variables for stata.dta", replace

use "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\All 10 MSAs\all10 20 size both refi_frm generated.dta", clear
keep if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0
compress
sort origmonth state
save "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\All 10 MSAs\TEMP all10 20 size both refi_frm generated.dta", replace
merge origmonth state using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\apl law variables for stata.dta"

egen big_loan_num = group(loan_id)

drop loan_id pp_pen pp_term state mba_stat currmonth origmonth mba_stat_lead1 outcome_b outsample_b outcome_c hpa init_rate orig_risk_prem subprime loan_id_num flip_loantype anti_steering

generate apl_pre = flip_duration * prepay_pen
generate apl_pre_end = flip_duration * prepay_pen_end
generate apl_bal = flip_duration * balloon
*generate apl_doc = flip_duration * lownodoc
compress

generate msa_atl = 0
generate msa_bal = 0
generate msa_chi = 0
generate msa_los = 0
generate msa_mia = 0
generate msa_min = 0
generate msa_new = 0
generate msa_pho = 0
generate msa_pit = 0
generate msa_san = 0
replace msa_atl = 1 if msa_num == 1
replace msa_bal = 1 if msa_num == 2
replace msa_chi = 1 if msa_num == 3
replace msa_los = 1 if msa_num == 4
replace msa_mia = 1 if msa_num == 5
replace msa_min = 1 if msa_num == 6
replace msa_new = 1 if msa_num == 7
replace msa_pho = 1 if msa_num == 8
replace msa_pit = 1 if msa_num == 9
replace msa_san = 1 if msa_num == 10
compress

generate pre_atl = prepay_pen * msa_atl
generate pre_bal = prepay_pen * msa_bal
generate pre_chi = prepay_pen * msa_chi
generate pre_los = prepay_pen * msa_los
generate pre_mia = prepay_pen * msa_mia
generate pre_min = prepay_pen * msa_min
generate pre_new = prepay_pen * msa_new
generate pre_pho = prepay_pen * msa_pho
generate pre_pit = prepay_pen * msa_pit
generate pre_san = prepay_pen * msa_san
compress

generate pre_end_atl = prepay_pen_end * msa_atl
generate pre_end_bal = prepay_pen_end * msa_bal
generate pre_end_chi = prepay_pen_end * msa_chi
generate pre_end_los = prepay_pen_end * msa_los
generate pre_end_mia = prepay_pen_end * msa_mia
generate pre_end_min = prepay_pen_end * msa_min
generate pre_end_new = prepay_pen_end * msa_new
generate pre_end_pho = prepay_pen_end * msa_pho
generate pre_end_pit = prepay_pen_end * msa_pit
generate pre_end_san = prepay_pen_end * msa_san
compress

generate bal_atl = balloon * msa_atl
generate bal_bal = balloon * msa_bal
generate bal_chi = balloon * msa_chi
generate bal_los = balloon * msa_los
generate bal_mia = balloon * msa_mia
generate bal_min = balloon * msa_min
generate bal_new = balloon * msa_new
generate bal_pho = balloon * msa_pho
generate bal_pit = balloon * msa_pit
generate bal_san = balloon * msa_san
compress

generate doc_atl = lownodoc * msa_atl
generate doc_bal = lownodoc * msa_bal
generate doc_chi = lownodoc * msa_chi
generate doc_los = lownodoc * msa_los
generate doc_mia = lownodoc * msa_mia
generate doc_min = lownodoc * msa_min
generate doc_new = lownodoc * msa_new
generate doc_pho = lownodoc * msa_pho
generate doc_pit = lownodoc * msa_pit
generate doc_san = lownodoc * msa_san
compress



*************************
*** APL variable only ***
*************************

*** Generating matrix of starting values using values from regression without APL variables ***
matrix starter = (0,0.2549496,-0.4605342,0.6474845,0.217507,0.2071075,-0.0121115,0.0414557,5.499194,0.1594637,-0.0019062,0.1762848,0.0553878,0.0060756,-0.5863241,-0.1413345,-0.0009571,0.291639,0.4907594,-0.1010615,-0.0149276,0.4687296,0.4327699,0.49238,0.7157436,0.6674291,0.4203297,-0.0222052,-0.8318148,-0.4632913,-0.4836892,-0.6408857,-0.7759935,-0.4075989,-0.6737114,-0.5306955,-0.5787229,-0.308549,-2.202192,-0.2326842,-0.0551499,0.1288478,0.1090464,0.8317115,0.8064796,0.3930382,1.820749,-0.4934099,-0.8634611,-0.8433856,0.4414436,-0.2120665,-0.7596715,-0.6596763,-0.379674,-1.040235,0.3182584,0.1869876,0.7044536,0.3770649,0.6329563,0.40605,0.394099,0.3037901,0.0129841,0,-0.1333012,0.1489448,-0.1073089,-0.0405351,0.0882499,-0.0018286,0.0034911,2.895189,0.0522556,-0.0011271,0.0288009,-0.1177945,0.0226472,0.1839612,-0.3073925,-0.4317431,-0.7115065,-0.8999835,0.1032508,-0.3100599,0.1545587,-0.0517149,-0.5641915,0.138253,-0.1226399,0.1348127,-0.7831268,-0.9389773,-0.3483144,-0.1870353,-0.477426,-0.166152,-0.0209769,-0.6031858,-0.3229663,-0.3796846,-0.3861235,-0.0537613,0.0522541,0.1652546,0.0436359,-0.3127503,0.453677,0.1573787,0.011376,0.249155,-0.0029223,0.0296526,0.0857446,0.0863092,0.0259329,-0.0164708,-0.1184376,-0.1881545,-0.2612402,0.1395119,-0.0962,0.266897,0.0540306,0.0612675,0.0404708,-0.0029188,0.0220061,-0.084449,-7.873841,-0.5194182,3.941156,-4.198967,-0.5768322)

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = flip_duration prepay_pen prepay_pen_end balloon lownodoc cashout fico cltv refi_premium ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_fixed vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san bal_atl bal_bal bal_chi bal_mia bal_min bal_new bal_pho bal_pit bal_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: flip_duration prepay_pen prepay_pen_end balloon lownodoc cashout fico cltv refi_premium ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_fixed vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san bal_atl bal_bal bal_chi bal_mia bal_min bal_new bal_pho bal_pit bal_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter, copy
ml maximize, iterate(50) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size refi_frm flip_duration.txt", replace onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- FRMs.do"

*** Saving the matrix of coefficient estimates ***
matrix a = e(b)

*** Creating starter matrices with 0 values for the APL*PLP interaction term coefficient estimates ***
*** -- This uses e(df_m), which is the number of explanatory variables in each equation, not       ***
***    including constant terms.                                                                   ***
*** -- For prepay_pen regressions, the APL variable is interacted with both prepay_pen and         ***
***    prepay_pen_end, requiring an extra pair of APL*PLP interaction term coefficient estimates.  ***
matrix afirst = a[1,1]
matrix asecond = a[1,2..(e(df_m)+1)]
matrix athird = a[1,(e(df_m)+2)..((e(df_m)*2)+5)]
matrix starter1 = (afirst,0,0,asecond,0,0,athird)
matrix starter2 = (afirst,0,asecond,0,athird)



*********************************************
*** APL*prepay_pen and APL*prepay_pen_end ***
*********************************************

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = flip_duration apl_pre apl_pre_end prepay_pen prepay_pen_end balloon lownodoc cashout fico cltv refi_premium ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_fixed vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san bal_atl bal_bal bal_chi bal_mia bal_min bal_new bal_pho bal_pit bal_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: flip_duration apl_pre apl_pre_end prepay_pen prepay_pen_end balloon lownodoc cashout fico cltv refi_premium ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_fixed vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san bal_atl bal_bal bal_chi bal_mia bal_min bal_new bal_pho bal_pit bal_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter1, copy
ml maximize, iterate(50) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size refi_frm flip_duration.txt", append onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- FRMs.do"



*******************
*** APL*balloon ***
*******************

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = flip_duration apl_bal prepay_pen prepay_pen_end balloon lownodoc cashout fico cltv refi_premium ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_fixed vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san bal_atl bal_bal bal_chi bal_mia bal_min bal_new bal_pho bal_pit bal_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: flip_duration apl_bal prepay_pen prepay_pen_end balloon lownodoc cashout fico cltv refi_premium ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_fixed vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san bal_atl bal_bal bal_chi bal_mia bal_min bal_new bal_pho bal_pit bal_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter2, copy
ml maximize, iterate(50) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size refi_frm flip_duration.txt", append onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- FRMs.do"

/*

********************
*** APL*lownodoc ***
********************

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = flip_duration apl_doc prepay_pen prepay_pen_end balloon lownodoc cashout fico cltv refi_premium ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_fixed vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san bal_atl bal_bal bal_chi bal_mia bal_min bal_new bal_pho bal_pit bal_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: flip_duration apl_doc prepay_pen prepay_pen_end balloon lownodoc cashout fico cltv refi_premium ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_fixed vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san bal_atl bal_bal bal_chi bal_mia bal_min bal_new bal_pho bal_pit bal_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter2, copy
ml maximize, iterate(50) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size refi_frm flip_duration.txt", append onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- FRMs.do"

*/

log close



program drop mymlogit_uh_lf



clear



